import { Controller } from "../../mvc/core/Controller";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { ServerCommand } from "../mvc/ServerCommand";
import { EGateCommand } from "../../../Message/Command/EGateCommand";
import { IView } from "../../mvc/interfaces/IView";
import { PetHireModule } from "../../../modules/PetHireModule";
import { WindowEvent } from "../../component/window/WindowEvent";
import { DataEvent } from "../events/DataEvent";
import { EPetRentQueryOrder } from "../../../Message/Game/EPetRentQueryOrder";
import { GameProxy } from "../mvc/GameProxy";
import { SPetRentInfos } from "../../../Message/Game/SPetRentInfos";
import { PetUtil } from "../view/pet/info/PetUtil";
import { MessageBlock } from "../../../Framework/MQ/MessageBlock";
import { SPetRentResult } from "../../../Message/Game/SPetRentResult";
import { EPetRentResult } from "../../../Message/Game/EPetRentResult";
import { MsgManager } from "../manager/MsgManager";
import { Language } from "../../../extend/language/Language";
import { SPetInfo } from "../../../Message/Game/SPetInfo";
import { Alert } from "../../../com/mui/controls/Alert";
import { EPlayerItemPosType } from "../../../Message/Public/EPlayerItemPosType";
import { SPetRentWallet } from "../../../Message/Game/SPetRentWallet";
import { SPlayerPetRentLog } from "../../../Message/Game/SPlayerPetRentLog";
import { IPetHireModule } from "../../../modules/interfaces/IPetHireModule";
import { _EPlayerItemPosTypePet } from "../../../EPlayerItemPosType/_EPlayerItemPosTypePet";
type int = number;
//class PetHireController
    
    export  class PetHireController extends Controller
    {
        constructor()
        {
            
            super();Dispatcher.addEventListener(EventName.PetHireSearch, this.onPetHireSearchHandler);
            Dispatcher.addEventListener(EventName.PetHireGetApply, this.onPetHireGetApplyHandler);
            Dispatcher.addEventListener(EventName.PetHireApply, this.onPetHireApplyHandler);
            Dispatcher.addEventListener(EventName.PetHireBorrow, this.onPetHireBorrowHandler);
            Dispatcher.addEventListener(EventName.PetHireFire, this.onPetHireFireHandler);
            Dispatcher.addEventListener(EventName.PetHireViewDetail, this.onPetHireViewDetailHandler);
            Dispatcher.addEventListener(EventName.PetHireGetMoney, this.onPetHireGetMoneyHandler);
            Dispatcher.addEventListener(EventName.PetReleaseSuccess, this.onPetReleaseSuccessHandler);
            Dispatcher.addEventListener(EventName.PetHireGetLog, this.onPetHireGetLogHandler);
            Dispatcher.addEventListener(EventName.PetHireChangeRentPrice, this.onPetHireChangeRentPriceHandler);
            return;
        }

        protected /* override */ initServer(): void
        {
            super.initServer();
            NetDispatcher.addCmdListener(ServerCommand.PetHireMyApplyCommand, this.onPetHireMyApplyCommandHandler);
            NetDispatcher.addCmdListener(ServerCommand.PetHireSearchCommand, this.onPetHireSearchCommandHandler);
            NetDispatcher.addCmdListener(EGateCommand._ECmdGatePetRentRentPet, this.onPetHireApplyReturnHandler);
            NetDispatcher.addCmdListener(EGateCommand._ECmdGatePetRentBorrowPet, this.onPetHireBorrowReturnHandler);
            NetDispatcher.addCmdListener(ServerCommand.PetHireQueryMyWalletCommand, this.onPetHireWalletUpdateHandler);
            NetDispatcher.addCmdListener(ServerCommand.PetHireGetMyWalletCommand, this.onPetHireWalletUpdateHandler);
            NetDispatcher.addCmdListener(ServerCommand.PetHireLogCommand, this.PetHireLogCommandHandler);
            NetDispatcher.addCmdListener(ServerCommand.PetHirePriceChangedCommand, this.onPetHirePriceChangedCommandHandler);
            NetDispatcher.addCmdListener(ServerCommand.PetHireChangePriceResultCommand, this.onPetHireChangePriceResultCommandHandler);
            return;
        }

        protected /* override */ initView(): IView
        {
            if (this._hireModule == null) 
            {
                this._hireModule = new PetHireModule();
                this._hireModule.addEventListener(WindowEvent.SHOW, this.onAddedToStageHandler);
            }
            return this._hireModule;
        }

        /* internal  */onAddedToStageHandler(arg1: Object /* flash.events.Event */): void
        {
            this._hireModule.showTabByIndex(0);
            return;
        }

        public /* override */ get view(): IView
        {
            return super.view;
        }

        /* internal  */onPetHireSearchHandler(arg1: DataEvent): void
        {
            var loc1=0;
            var loc2=0;
            var loc3=0;
            var loc4=0;
            var loc5=true;
            var loc6=false;
            var loc7=false;
            var loc8=EPetRentQueryOrder._EPetRentQueryOrderTimeDsc;
            var loc9=1;
            var loc10;
            if ((loc10 = arg1.data) != null) 
            {
                loc1 = int(loc10["level"]);
                loc2 = int(loc10["talent"]);
                loc3 = int(loc10["spirit"]);
                loc4 = int(loc10["growth"]);
                loc8 = int(loc10["queryOrder"]);
                loc9 = int(loc10["targetPage"]);
                loc5 = loc10["isAbleHire"] as Boolean;
                loc6 = loc10["isOnlyGold"] as Boolean;
                loc7 = loc10["isOnlyCoin"] as Boolean;
            }
            GameProxy.petHireProxy.search(loc1, loc2, loc3, loc4, loc5, loc6, loc7, loc8, loc9);
            return;
        }

        /* internal  */onPetHireGetApplyHandler(arg1: DataEvent): void
        {
            GameProxy.petHireProxy.getMyRents();
            return;
        }

        /* internal  */onPetHireApplyHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data["uid"] as string;
            var loc2=arg1.data["price"] as int;
            var loc3=arg1.data["priceUnit"] as int;
            var loc4=arg1.data["vision"] as int;
            GameProxy.petHireProxy.rentPet(loc1, loc2, loc3, loc4);
            return;
        }

        /* internal  */onPetHireBorrowHandler(arg1: DataEvent): void
        {
            var loc1=Number(arg1.data["id"]);
            var loc2=arg1.data["price"] as int;
            var loc3=arg1.data["unit"] as int;
            GameProxy.petHireProxy.borrowPet(loc1, loc2, loc3);
            return;
        }

        /* internal  */onPetHireMyApplyCommandHandler(arg1: SPetRentInfos): void
        {
            var loc1=this.getApplySortArray(cache.pet.petInfos);
            if (this._hireModule) 
            {
                this._hireModule.updateApplyPanel(loc1);
            }
            return;
        }

        /* internal  */getApplySortArray(arg1: Array<any>): Array<any>
        {
            var loc4=null;
            var loc5=null;
            var loc1=[];
            var loc2=[];
            var loc3=[];
            var loc6=0;
            var loc7=arg1;
            for(loc5 of loc7) 
            {
                if (loc5.isBorrow != false) 
                {
                    continue;
                }
                if (loc4 = cache.petHire.getApplyedPetRent(loc5.uid)) 
                {
                    loc1.push(loc5);
                    continue;
                }
                if (PetUtil.isPetCanApply(loc5)) 
                {
                    loc3.push(loc5);
                    continue;
                }
                loc2.push(loc5);
            }
            loc1 = loc1.concat(loc3).concat(loc2);
            return loc1;
        }

        /* internal  */onPetHireSearchCommandHandler(arg1: SPetRentInfos): void
        {
            if (this._hireModule) 
            {
                this._hireModule.updateMarketPanel(arg1);
            }
            return;
        }

        /* internal  */onPetHireApplyReturnHandler(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SPetRentResult;
            if (loc1.result != EPetRentResult._EPetRentResultSuccess) 
            {
                MsgManager.showRollTipsMsg(Language.getString(54406));
            }
            else 
            {
                MsgManager.showRollTipsMsg(Language.getString(54376));
                this.onPetHireGetApplyHandler(null);
            }
            return;
        }

        /* internal  */onPetHireBorrowReturnHandler(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SPetRentResult;
            if (loc1.result != EPetRentResult._EPetRentResultSuccess) 
            {
                MsgManager.showRollTipsMsg(Language.getString(54407));
            }
            else 
            {
                MsgManager.showRollTipsMsg(Language.getString(54378));
            }
            return;
        }

        /* internal  */onPetHireFireHandler(arg1: DataEvent): void
        {
            var e: DataEvent;
            var petInfo: SPetInfo;
            var onBtnClickHandler: Function;

            var loc1;
            petInfo = null;
            e = arg1;
            petInfo = e.data as SPetInfo;
            if (petInfo) 
            {
                onBtnClickHandler = (arg1: int): void
                {
                    if (arg1 == Alert.OK) 
                    {
                        Dispatcher.dispatchEvent(new DataEvent(EventName.PetRelease, {"uid": petInfo.uid, "posType": _EPlayerItemPosTypePet}));
                        Alert.resetToDefault();
                    }
                    return;
                }
                Alert.show(Language.getStringByParam(54379, PetUtil.getPetName(petInfo)), null, Alert.OK | Alert.CANCEL, null, onBtnClickHandler);
            }
            return;
        }

        /* internal  */onPetHireViewDetailHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as number;
            GameProxy.petHireProxy.petRentDetailInfo(loc1);
            return;
        }

        /* internal  */onPetHireGetMoneyHandler(arg1: DataEvent): void
        {
            GameProxy.petHireProxy.getEarnMoney();
            return;
        }

        /* internal  */onPetHireWalletUpdateHandler(arg1: SPetRentWallet): void
        {
            if (this._hireModule && arg1) 
            {
                this._hireModule.updateMoney(arg1);
            }
            return;
        }

        /* internal  */onPetReleaseSuccessHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data.posType;
            if (loc1 == EPlayerItemPosType._EPlayerItemPosTypePet) 
            {
                if (this._hireModule) 
                {
                    this._hireModule.updateMyPanel();
                }
            }
            return;
        }

        /* internal  */PetHireLogCommandHandler(arg1: SPlayerPetRentLog): void
        {
            if (this._hireModule && arg1) 
            {
                this._hireModule.updateHireLog(arg1);
            }
            return;
        }

        /* internal  */onPetHireGetLogHandler(arg1: DataEvent): void
        {
            GameProxy.petHireProxy.getPetRentLog();
            return;
        }

        /* internal  */onPetHireChangeRentPriceHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data["uid"] as string;
            var loc2=arg1.data["price"] as int;
            var loc3=arg1.data["unit"] as int;
            GameProxy.petHireProxy.changeRentPrice(loc1, loc2, loc3);
            return;
        }

        /* internal  */onPetHirePriceChangedCommandHandler(arg1: Object): void
        {
            MsgManager.showRollTipsMsg(Language.getString(54598));
            if (this._hireModule && this._hireModule.isHide == false) 
            {
                this._hireModule.refresh();
            }
            return;
        }

        /* internal  */onPetHireChangePriceResultCommandHandler(arg1: SPetRentResult): void
        {
            if (arg1 && arg1.result == EPetRentResult._EPetRentResultSuccess) 
            {
                MsgManager.showRollTipsMsg(Language.getString(54597));
            }
            return;
        }

        private /* var */_hireModule: IPetHireModule;
    }
